public class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        HeroNode node5_1 = new HeroNode(5, "艾比");
        root.setLeft(node2);
        node2.setLeft(node5_1);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        tree.setRoot(root);
        System.out.println("======前序遍历======");
        tree.preOrder();
        System.out.println("======中序遍历======");
        tree.infixOrder();
        System.out.println("======后序遍历======");
        tree.postOrder();
        System.out.println("=====================");
        //HeroNode heroNode = tree.preSearch(1);
        //System.out.println(heroNode);
        tree.delNode(5);

    }

    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public HeroNode preSearch(int no) {
        if (this.root != null) {
            return root.preSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixSearch(int no) {
        if (this.root != null) {
            return root.infixSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode postSearch(int no) {
        if (this.root != null) {
            return root.postSearch(no);
        } else {
            return null;
        }
    }

    public void delNode(int no) {
        if (root != null) {
            if (root.getNo() == no) {
                root = null;
            } else {
                root.deleteNode(no);
            }
        } else {
            System.out.println("该树为空");
        }
    }


}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public HeroNode preSearch(int no) {
        if (this.no == no) {
            return this;
        }
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.right != null) {
            return this.right.preSearch(no);
        }
        return null;
    }

    public HeroNode infixSearch(int no) {
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            return this.right.preSearch(no);
        }
        return null;
    }

    public HeroNode postSearch(int no) {
        if (this.left != null) {
            if (this.left.preSearch(no) != null) {
                return this.left.preSearch(no);
            }
        }
        if (this.right != null) {
            if (this.right.preSearch(no) != null) {
                return this.right.preSearch(no);
            }
        }
        if (this.no == no) {
            return this;
        }

        return null;
    }

    //删除结点,会删除所有编号=no的子树
    public void deleteNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.left = null;
            return;
        }
        if (this.left != null) {
            this.left.deleteNode(no);
        }
        if (this.right != null) {
            this.right.deleteNode(no);
        }
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public HeroNode getRight() {
        return right;
    }
}
